package com.gxa.employment.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gxa.employment.common.Result;
import com.gxa.employment.entity.dto.PageBean;
import com.gxa.employment.entity.pojo.Clazz;
import com.gxa.employment.entity.pojo.Major;
import com.gxa.employment.entity.pojo.Staff;
import com.gxa.employment.entity.pojo.Staff;
import com.gxa.employment.entity.vo.PageAndListVo;
import com.gxa.employment.entity.vo.PageVo;
import com.gxa.employment.service.StaffService;
import com.gxa.employment.service.StaffService;
import com.gxa.employment.service.impl.ClazzServiceImpl;
import com.gxa.employment.service.impl.MajorServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

/**
 * <p>
 * 员工信息表 前端控制器
 * </p>
 *
 * @author author
 * @since 2025-03-25
 */
@RestController
@RequestMapping("/staff")
@Tag(name = " 员工管理", description = " 员工相关操作API")
public class StaffController {
    @Autowired
    private StaffService staffService;
    @Autowired
    private MajorServiceImpl majorServiceImpl;


    /**
     * 添加方向接口
     * 该接口用于添加一个新的方向信息，并将该信息保存到数据库中。
     *
     * @param staff 要添加的方向信息，封装在Staff对象中
     * @return 返回操作结果，成功则返回成功标识
     */
    @PostMapping("/add")
    @Operation(summary = "添加员工")
    public Result<Staff> add(@RequestBody Staff staff){
        staffService.save(staff);
        return Result.success();
    }
    /**
     * 修改员工信息
     * 通过传入的员工对象更新员工信息
     *
     * @param staff 员工对象，包含需要更新的员工信息
     * @return 返回操作结果，成功则返回成功结果
     */

    @PutMapping("/update")
    @Operation(summary = "修改员工")
    public Result<Staff> update(@RequestBody Staff staff){
        staffService.updateById(staff);
        return Result.success();
    }

    /**
     * 删除员工
     * 根据员工ID删除对应的员工信息
     *
     * @param id 员工ID
     * @return 操作结果
     */
    @DeleteMapping("/delete")
    @Operation(summary = "修改员工")
    public Result<Staff> delete(@RequestParam Integer id){
        staffService.removeById(id);
        return Result.success();
    }


    @GetMapping("/queryContact")
    @Operation(summary = "根据联系方式查询员工详情")
    public Result<Staff> getById(@RequestParam String staffContact){
        LambdaQueryWrapper<Staff> lambdaQuery = Wrappers.lambdaQuery(Staff.class);
        lambdaQuery.eq(Staff::getStaffContact, staffContact);
        Staff one = staffService.getOne(lambdaQuery);
        return Result.success(one);
    }

    /**
     * 获取员工列表
     * 该接口用于获取所有员工的列表数据
     *
     * @return 返回包含员工列表的Result对象
     */
    @PostMapping("/getByPage")
    @Operation(summary = "分页模糊查询员工信息和班级名称")
    public Result<PageAndListVo> getByPage(@RequestBody PageBean pageBean){
        String name = pageBean.getName();
        //查询条件
        LambdaQueryWrapper<Staff> lambdaQuery = new LambdaQueryWrapper<>();
        lambdaQuery.like(StringUtils.isNotEmpty(name),Staff::getStaffName,name);
        //分页
        IPage<Staff> page = new Page<>(pageBean.getPage(), pageBean.getSize());
        staffService.page(page, lambdaQuery);
        PageVo<Staff> pageVo = new PageVo<>(page.getTotal(), page.getRecords());
        //班级信息
        List<Major> list = majorServiceImpl.list();
        return Result.success(new PageAndListVo(pageVo,list));
    }
}
